<?php

/*

  Action	$3

  +1 Action
  Look at the top 3 cards of your deck.
  Trash one of them. Discard one of them.
  Put the other one on top of your deck.

 */

class Lookout extends Card implements iAction, iKingdom, iSeaside {

    public function __construct() {
        parent::__construct("Lookout", 3);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+1 Action",
            "Look at the top 3 cards of your deck.  Trash one of them. Discard one of them.  Put the other one on top of your deck.");
    }

    public function prepareCards($cardToSkip, $cards) {
        $skiped_card = false;

        foreach ($cards as $card) {
            $card = prepare($card);
            if ($cardToSkip != null && $card->getKey() == $cardToSkip->getKey() && !$skiped_card) {
                $skiped_card = true;
            } else {
                $cards_obj[] = $card;
            }
        }
        return $cards_obj;
    }

    public function play(Game $game, $isRealCard) {
        $game->addActions(1);
        ScreenDisplay::addScreenMessage($game, true, "get  +1 action", "gets +1 action");

        $cards = $game->removeCardsFromDrawDeck(3);
        ScreenDisplay::addScreenMessage($game, true, "pick " . nicePrint($cards), "picks " . sizeof($cards) . " cards");
        if (sizeof($cards) > 1) {
            $cards = $this->prepareCards(null, $cards);


            $output = MySocket::GetUserReply($game, Input_ChooseOne, $cards, 1, 1, null, "Choose card to trash");

            $card_name = $output[0];
            $trashed_card = $game->putCardInTrash($card_name);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_card), "trashes " . nicePrint($trashed_card));

            $cards = $this->prepareCards($trashed_card, $cards);

            if (sizeof($cards) > 1) {


                $output = MySocket::GetUserReply($game, Input_ChooseOne, $cards, 1, 1, null, "Choose card to discard");

                $card_name = $output[0];
                $discarded_card = $game->putCardInDiscardDeck($card_name);
                ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($discarded_card), "discards 1 card ");

                $cards = $this->prepareCards($discarded_card, $cards);

                if (sizeof($cards) == 1) {
                    $game->putCardOnTopOfDrawDeck($cards[0]);
                    ScreenDisplay::addScreenMessage($game, true, "return " . nicePrint($cards[0]) . " on top of your deck", "returns 1 card on top of his deck");
                }
            } else if (sizeof($cards) == 1) {
                $game->putCardInDiscardDeck($cards[0]);
                ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($cards[0]), "discards 1 card ");
            }
        } else if (sizeof($cards) == 1) {
            $game->putCardInTrash($cards[0]);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($cards[0]), "trashes " . ScreenDisplay::$LMSG);
        }
        $game->cardActionFinished();
        return true;
    }

}

?>